package com.lin.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lin.dataobject.ProductAttributeValue;
import com.lin.dto.NewProductDTO;
import com.lin.dto.ProductDTO;
import com.lin.form.NewProductForm;
import com.lin.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017-11-03.
 */
@Slf4j
public class ProductForm2ProductDTOConverter {

    public static NewProductDTO convert(NewProductForm form){
        Gson gson=new Gson();
        NewProductDTO dto=new NewProductDTO();
        if (StringUtils.isNotEmpty(form.getId())){
            dto.setId(form.getId());
        }
        dto.setName(form.getName());
        dto.setRemark(form.getRemark());
        dto.setCategory(form.getCategory());
        dto.setBrand(form.getBrand());

        List<ProductAttributeValue> list=new ArrayList<>();
        try {
            list=gson.fromJson(form.getAttribute(),
                    new TypeToken<List<ProductAttributeValue>>(){}.getType());
        }catch (Exception e){
            log.error("【对象转换】错误, string={}", form.getAttribute());
        }
        dto.setList(list);

        return dto;
    }
}
